﻿package com.sff {
	import com.sff.core.sff_internal;
	import com.sff.manager.DataManager;
	import com.sff.module.Module;
	import com.sff.module.ModuleLoader;
	import com.sff.system.ModuleSystem;
	import com.sff.security.Security;
	import com.sff.site.address.SWFAddress;
	import com.sff.site.connector.SwfAddressConnector;
	import com.sff.site.tree.TreeConfig;
	import flash.external.ExternalInterface;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class SFFCore extends Module {
	
		public function SFFCore() {
		
		}
		
		protected function loadConfig(url:String):void {			
			var ldr:URLLoader = new URLLoader();
			ldr.addEventListener(Event.COMPLETE, onCompleteLoadConfigHandler);
			ldr.load(new URLRequest(url));
		}
		
		protected function onCompleteLoadConfigHandler(evt:Event):void {
			
			try {				
				var xml:XML = new XML(evt.currentTarget.data);				
								
				TreeConfig.start(xml);
				SwfAddressConnector.start( );
				Security.start();
				
				TreeConfig.instance.run( );
				SwfAddressConnector.instance.sff_internal::connectAddress( );
				
				SWFAddress.initialize();
				
				start();
				
			} catch (err:Error) {}			
		}
				
		protected function start():void {
			
			var xml:XML = new XML("<module><siteMap/><data/><static/></module>");	
			
			var data:DataManager = new DataManager(xml, null);
			var ms:ModuleSystem = new ModuleSystem(data, null, this);
			
			//there is no siteMap, data and static in root of site tree, so that init and run module
			sff_internal::_init(ms);
            sff_internal::_run();			
		
			loadDefaultRoot();
		}
		
		//load default root of main device
		protected function loadDefaultRoot():void {
			var ldr:ModuleLoader = system.getModuleLoader();
			ldr.addEventListener(Event.COMPLETE, onCompleteLoadDefaultRootHandler);
			ldr.load(new URLRequest(TreeConfig.instance.getDefaultRootUrl()), system.dataManager);
		}
		
		protected function onCompleteLoadDefaultRootHandler(evt:Event):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteLoadDefaultRootHandler);
			
			addChild(evt.currentTarget.module);
		
			loadRoots();
			
			stage.addEventListener(Event.RESIZE, onStageResizeHandler);
			onStageResizeHandler(null);
		}
		
		private function onStageResizeHandler(evt:Event):void {
			system.resizeManager.setSize(stage.stageWidth, stage.stageHeight);
		}
		
		//load root of other device
		protected function loadRoots(): void {
			
			var arrUrl:Array = TreeConfig.instance.getRootUrls();
			var ldr:ModuleLoader;
			
			for (var i:int = 0; i < arrUrl.length; i ++) {
				ldr = system.getModuleLoader();
				ldr.addEventListener(Event.COMPLETE, onCompleteLoadRootHandler);
				ldr.load(new URLRequest(arrUrl[i]), system.dataManager);
			}			
		}
		
		private function onCompleteLoadRootHandler(evt:Event):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteLoadRootHandler);
			
			addChild(evt.currentTarget.module);
		}	
	}
}